package com.taillive.commons;

import java.io.UnsupportedEncodingException;



public class TextUtils {

		public static String arrayToStr (int[] arr,String separator){
        if(arr==null||arr.length==0){
            return "";
        }

        StringBuffer buffer = new StringBuffer();

        for(int i=0;i<arr.length;i++){
            if(i!=0){
                buffer.append(separator);
            }
            buffer.append(arr[i]);
        }

        String str = buffer.toString();
        buffer.setLength(0);
        buffer=null;
        return str;				
		}
		
		
    public static String arrayToStr (String[] arr,String separator){
        if(arr==null||arr.length==0){
            return "";
        }

        StringBuffer buffer = new StringBuffer();

        for(int i=0;i<arr.length;i++){
            if(i!=0){
                buffer.append(separator);
            }
            buffer.append(arr[i]);
        }

        String str = buffer.toString();
        buffer.setLength(0);
        buffer=null;
        return str;
    }


    public static String[] strToArray(String str,String separator){
        if(str==null||str.equals("")){
            return null;
        }

        return str.split(separator);

    }
    
    public static String asciiToUTF8(String str){
    	if(str==null||"".equals(str.trim())){
    		return "";
    	}
    	try {
			return new String(str.getBytes("iso-8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			return "";
		}  
    }
    
    public static boolean isLetterString(String s){
    	if(s==null||s.length()==0){
    		return false;
    	}
    	
    	s = s.trim();
    	char[] ca = s.toCharArray();
    	for(int i =0;i<ca.length;i++){
    		if(!isLetter(ca[i])){
    			return false;
    		}
    	}
    	return true;
    		
    }
    
    public static boolean isLetter(char c){
    	return (c >='a' && c <='z' ) || (c >='A' && c <='Z' );
    }
    
    public static boolean isDigit(char c){
    	return c >= 48 && c <= 57;
    }
    
    
    public static boolean isEmpty(String s){
    	return s==null||"".equals(s.trim());
    }
    
    
    public static boolean isDigitString(String s){
	   	 for(int j = 0; j < s.length(); j++) {
	 	    if (!isDigit(s.charAt(j))) {
	 	    	return false;
	 	    }
	 	 }
	   	 return true;
    }

    
    public static String cut(String s,int length){
    	
    	if (s == null || s.length()<length) {
	        return s;
	    }else{
	    	return s.substring(0, length)+ " ...";
	    }
    }
    	
    	 
    	      	

    
}
